package cn.addapp.pickers.picker;

import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import java.util.Calendar;
import java.util.Locale;

import cn.addapp.pickers.adapter.ArrayWheelAdapter;
import cn.addapp.pickers.listeners.OnItemPickListener;
import cn.addapp.pickers.util.DateUtils;
import cn.addapp.pickers.util.LogUtils;
import cn.addapp.pickers.wheelpicker.R;
import cn.addapp.pickers.widget.WheelView;

/**
 * 日期选择器
 *
 * @author matt
 * blog: addapp.cn
 */
public class DatePicker extends DateTimePicker {

    public DatePicker(AppCompatActivity activity) {
        this(activity, YEAR_MONTH_DAY);
        setSelectedTextColor(ContextCompat.getColor(activity, R.color.orange));
        setUnSelectedTextColor(ContextCompat.getColor(activity, R.color.black));
    }

    /**
     * @see #YEAR_MONTH_DAY
     * @see #YEAR_MONTH
     * @see #MONTH_DAY
     */
    public DatePicker(AppCompatActivity activity, @DateMode int mode) {
        super(activity, mode, NONE);
    }

    /**
     * 设置年月日的单位
     */
    public void setLabel(String yearLabel, String monthLabel, String dayLabel) {
        super.setLabel(yearLabel, monthLabel, dayLabel, "", "");
    }

    /**
     * 设置范围：开始的年月日
     */
    public void setRangeStart(int startYear, int startMonth, int startDay) {
        super.setDateRangeStart(startYear, startMonth, startDay);
    }

    /**
     * 设置范围：结束的年月日
     */
    public void setRangeEnd(int endYear, int endMonth, int endDay) {
        super.setDateRangeEnd(endYear, endMonth, endDay);
    }

    /**
     * 设置范围：开始的年月日
     */
    public void setRangeStart(int startYearOrMonth, int startMonthOrDay) {
        super.setDateRangeStart(startYearOrMonth, startMonthOrDay);
    }

    /**
     * 设置范围：结束的年月日
     */
    public void setRangeEnd(int endYearOrMonth, int endMonthOrDay) {
        super.setDateRangeEnd(endYearOrMonth, endMonthOrDay);
    }


    /**
     * 设置默认选中的年月日
     */
    public void setSelectedItem(int year, int month, int day) {
        super.setSelectedItem(year, month, day, 0, 0);
    }

    /**
     * 设置默认选中的年月或者月日
     */
    public void setSelectedItem(int yearOrMonth, int monthOrDay) {
        super.setSelectedItem(yearOrMonth, monthOrDay, 0, 0);
    }


    public void setOnWheelListener(final OnWheelListener listener) {
        if (null == listener) {
            return;
        }
        super.setOnWheelListener(new DateTimePicker.OnWheelListener() {
            @Override
            public void onYearWheeled(int index, String year) {
                listener.onYearWheeled(index, year);
            }

            @Override
            public void onMonthWheeled(int index, String month) {
                listener.onMonthWheeled(index, month);
            }

            @Override
            public void onDayWheeled(int index, String day) {
                listener.onDayWheeled(index, day);
            }

            @Override
            public void onHourWheeled(int index, String hour) {

            }

            @Override
            public void onMinuteWheeled(int index, String minute) {

            }
        });
    }

    public void setOnDatePickListener(final OnDatePickListener listener) {
        if (null == listener) {
            return;
        }
        if (listener instanceof OnYearMonthDayPickListener) {
            super.setOnDateTimePickListener(new OnYearMonthDayTimePickListener() {
                @Override
                public void onDateTimePicked(String year, String month, String day, String hour, String minute) {
                    ((OnYearMonthDayPickListener) listener).onDatePicked(year, month, day);
                }
            });
        } else if (listener instanceof OnYearMonthPickListener) {
            super.setOnDateTimePickListener(new OnYearMonthTimePickListener() {
                @Override
                public void onDateTimePicked(String year, String month, String hour, String minute) {
                    ((OnYearMonthPickListener) listener).onDatePicked(year, month);
                }
            });
        } else if (listener instanceof OnMonthDayPickListener) {
            super.setOnDateTimePickListener(new OnMonthDayTimePickListener() {
                @Override
                public void onDateTimePicked(String month, String day, String hour, String minute) {
                    ((OnMonthDayPickListener) listener).onDatePicked(month, day);
                }
            });
        }
    }

    protected interface OnDatePickListener {

    }

    public interface OnYearMonthDayPickListener extends OnDatePickListener {

        void onDatePicked(String year, String month, String day);

    }

    public interface OnYearMonthPickListener extends OnDatePickListener {

        void onDatePicked(String year, String month);

    }

    public interface OnMonthDayPickListener extends OnDatePickListener {

        void onDatePicked(String month, String day);

    }

    public interface OnWheelListener {

        void onYearWheeled(int index, String year);

        void onMonthWheeled(int index, String month);

        void onDayWheeled(int index, String day);

    }

    @NonNull
    @Override
    protected View makeCenterView() {
        // 如果未设置默认项，则需要在此初始化数据
        if ((dateMode == YEAR_MONTH_DAY || dateMode == YEAR_MONTH) && years.size() == 0) {
            LogUtils.verbose(this, "init years before make view");
            initYearData();
        }
        if (dateMode != NONE && months.size() == 0) {
            LogUtils.verbose(this, "init months before make view");
            int selectedYear = DateUtils.trimZero(getSelectedYear());
            changeMonthData(selectedYear);
        }
        if ((dateMode == YEAR_MONTH_DAY || dateMode == MONTH_DAY) && days.size() == 0) {
            LogUtils.verbose(this, "init days before make view");
            int selectedYear;
            if (dateMode == YEAR_MONTH_DAY) {
                selectedYear = DateUtils.trimZero(getSelectedYear());
            } else {
                selectedYear = Calendar.getInstance(Locale.CHINA).get(Calendar.YEAR);
            }
            int selectedMonth = DateUtils.trimZero(getSelectedMonth());
            changeDayData(selectedYear, selectedMonth);
        }


        LinearLayout layout = new LinearLayout(activity);
        layout.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setGravity(Gravity.CENTER);

        LinearLayout.LayoutParams wheelViewParams = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
        LinearLayout.LayoutParams labelViewParams = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
        final WheelView yearView = new WheelView(activity);
        final WheelView monthView = new WheelView(activity);
        final WheelView dayView = new WheelView(activity);
        if (dateMode == YEAR_MONTH_DAY || dateMode == YEAR_MONTH) {
            yearView.setCanLoop(canLoop);
            yearView.setTextSize(textSize);//must be called before setDateList
            yearView.setSelectedTextColor(textColorFocus);
            yearView.setUnSelectedTextColor(textColorNormal);
            yearView.setLineConfig(lineConfig);
            yearView.setAdapter(new ArrayWheelAdapter<>(years));
            yearView.setCurrentItem(selectedYearIndex);
            wheelViewParams.weight = 1.0f;
            yearView.setLayoutParams(wheelViewParams);
            yearView.setOnItemPickListener(new OnItemPickListener<String>() {
                @Override
                public void onItemPicked(int index, String item) {
                    selectedYearIndex = index;
                    if (onWheelListener != null) {
                        onWheelListener.onYearWheeled(selectedYearIndex, item);
                    }
                    if (!canLinkage) {
                        return;
                    }
                    LogUtils.verbose(this, "change months after year wheeled");
                    selectedMonthIndex = 0;//重置月份索引
                    selectedDayIndex = 0;//重置日子索引
                    //需要根据年份及月份动态计算天数
                    int selectedYear = DateUtils.trimZero(item);
                    changeMonthData(selectedYear);
                    monthView.setAdapter(new ArrayWheelAdapter<>(months));
                    monthView.setCurrentItem(selectedMonthIndex);
                    changeDayData(selectedYear, DateUtils.trimZero(months.get(selectedMonthIndex)));
                    dayView.setAdapter(new ArrayWheelAdapter<>(days));
                    dayView.setCurrentItem(selectedDayIndex);
                }
            });
            layout.addView(yearView);
        }
        if (dateMode != NONE) {
            monthView.setCanLoop(canLoop);
            monthView.setTextSize(textSize);//must be called before setDateList
            monthView.setSelectedTextColor(textColorFocus);
            monthView.setUnSelectedTextColor(textColorNormal);
            monthView.setAdapter(new ArrayWheelAdapter<>(months));
            monthView.setLineConfig(lineConfig);
            monthView.setCurrentItem(selectedMonthIndex);
            wheelViewParams.weight = 1.0f;
            monthView.setLayoutParams(wheelViewParams);
            monthView.setOnItemPickListener(new OnItemPickListener<String>() {
                @Override
                public void onItemPicked(int index, String item) {
                    selectedMonthIndex = index;
                    if (onWheelListener != null) {
                        onWheelListener.onMonthWheeled(selectedMonthIndex, item);
                    }
//                        if (!canLinkage) {
//                            return;
//                        }
                    if (dateMode == YEAR_MONTH_DAY || dateMode == MONTH_DAY) {
                        LogUtils.verbose(this, "change days after month wheeled");
                        selectedDayIndex = 0;//重置日子索引
                        int selectedYear;
                        if (dateMode == YEAR_MONTH_DAY) {
                            selectedYear = DateUtils.trimZero(getSelectedYear());
                        } else {
                            selectedYear = Calendar.getInstance(Locale.CHINA).get(Calendar.YEAR);
                        }
                        changeDayData(selectedYear, DateUtils.trimZero(item));
                        dayView.setAdapter(new ArrayWheelAdapter<>(days));
                        dayView.setCurrentItem(selectedDayIndex);
                    }
                }
            });
            layout.addView(monthView);
        }
        if (dateMode == YEAR_MONTH_DAY || dateMode == MONTH_DAY) {
            dayView.setCanLoop(canLoop);
            dayView.setTextSize(textSize);//must be called before setDateList
            dayView.setSelectedTextColor(textColorFocus);
            dayView.setUnSelectedTextColor(textColorNormal);
            dayView.setAdapter(new ArrayWheelAdapter<>(days));
            dayView.setCurrentItem(selectedDayIndex);
            dayView.setLineConfig(lineConfig);
            wheelViewParams.weight = 1f;
            dayView.setLayoutParams(wheelViewParams);
            dayView.setOnItemPickListener(new OnItemPickListener<String>() {
                @Override
                public void onItemPicked(int index, String item) {
                    selectedDayIndex = index;
                    if (onWheelListener != null) {
                        onWheelListener.onDayWheeled(index, item);
                    }
                }
            });
            layout.addView(dayView);
        }
        return layout;
    }
}
